<!-- 个人信息-基本资料 子路由组件-elementUI的Card 卡片实现页面制作 -->
<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>基本资料</span>
      </div>
      <!-- 表单(1.rules 属性传入约定的验证规则 2.ref 操作Dom 3.model表单提交) -->
      <el-form :model="userForm" :rules="userFormRules" ref="userFormRef" label-width="100px" class="demo-userForm">
        <el-form-item label="登录名称" prop="username">
          <el-input v-model="userForm.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickname">
          <el-input v-model="userForm.nickname"></el-input>
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="userForm.email"></el-input>
        </el-form-item>
        <el-form-item size="large">
          <el-button type="primary" @click="submitFn">提交修改</el-button>
          <el-button @click="reseFn">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
// 1.引入更新用户信息数据的接口
import { updateUserInfoAPI } from '@/api/index'
export default {
  name: 'my-userinfo',
  data() {
    return {
      // 表单数据对象
      userForm: {
        username: this.$store.state.userInfo.username, // 登录后获取到的用户名
        nickname: '',
        email: ''
      },
      // 表单的验证规则对象
      userFormRules: {
        nickname: [
          { required: true, message: '请输入用户昵称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '昵称必须是1-10位的非空字符串', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    // 提交修改-》点击事件
    submitFn() {
      this.$refs.userFormRef.validate(async valid => {
        if (valid) {
          // 通过校验
          // console.log(this.userForm)
          // 1.调用接口
          this.userForm.id = this.$store.state.userInfo.id // id也是必传项
          const { data: res } = await updateUserInfoAPI(this.userForm)
          // console.log(res)
          // 2.失败,提示用户
          if (res.code !== 0) return this.$message.error(res.message)
          // 3.成功,提示用户
          this.$message.success({
            message: `${res.message}`,
            duration: 2000,
            customClass: 'windthClass',
            onClose: () => {
              this.$refs.userFormRef.resetFields()
            }
          })
          // 5.重新让vuex获取最新的用户信息数据
          this.$store.dispatch('getUserInfoActions')
        } else {
          // 阻止默认的提交行为(表单下面的红色提示会自动出现)
          return false
        }
      })
    },
    // 重置-》点击事件
    reseFn() {
      // 重置表单的数据且还重置了校验报错的提示
      this.$refs.userFormRef.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.clearfix span {
  font-size: 16px;
  font-weight: 700;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: '';
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 100%;
  background-color: rgba(242, 242, 242, 0.6);
}
.el-form {
  width: 500px;
}
</style>
